using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Tester.GuiActions.Actions
{
    public class ActionsReader
    {
        private string _file;
        private Actions _actions; 

        public ActionsReader(string file)
        {
            _file = file; 
        }

        public void ReadActions(Actions actions)
        {
            _actions = actions; 
            using (XmlReader reader = XmlReader.Create(_file))
            {
                Read(reader);
            }
        }

        private void Read(XmlReader reader)
        {
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        {
                            switch (reader.Name)
                            {
                                case "TesterActions":
                                    Read(reader);
                                    return; 
                                case "MainMenuItems":
                                    _actions.ReadMenu(reader);
                                    break;
                                case "Contexts":
                                    _actions.ReadContexts(reader);
                                    break;
                                case "Action":
                                    _actions.ReadActions(reader);
                                    break;
                                default:
                                    throw new Exception("xml reader error");
                            }
                        }                        
                        break;
                    case XmlNodeType.EndElement:
                        return;
                    default:
                        break;
                }
            }
        }
    }
}
